package com.example.rwq_test.aspect;

import com.alibaba.fastjson.JSON;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Method;

/**
 * TODO note
 *
 * @author renweiqiang
 * @version 1.0.0
 * @createTime 2023/1/31
 */
@Aspect
@RestController
public class ControllerAspect {
    @Pointcut("execution (public * com.example.rwq_test.controller.*Controller.*(..))")
//    @Pointcut("@annotation(com.example.rwq_test.annotation.ControllerAround)")
    public void controllerAround(){

    }

    @Around("controllerAround()")
    public Object controllerAround(ProceedingJoinPoint proceedingJoinPoint) {
        MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
        Method method = signature.getMethod();

        String className = proceedingJoinPoint.getTarget().getClass().getName();
        String methodName = method.getName();
        Object[] args = proceedingJoinPoint.getArgs();
        System.out.println(className + "." + methodName + ": " + JSON.toJSONString(args));
        Object result =null;
        try {
            result = proceedingJoinPoint.proceed();
        } catch (Throwable e) {
            System.out.println(e.getMessage());
        }
        if(result != null){
            System.out.println("result: " + result);
        }
        return result;
    }
}
